(function() {
  app.controller('detailsCtrl', function($filter, $state, $scope, $rootScope, tradeApi, quoteApi, local_m, alertTip, $window, $timeout) {
    $scope.run_details = local_m.get('running');
    console.log($scope.run_details)
    $scope.init = function() {
      if ($scope.run_details.contract_status === '6') {
        $scope.sell_button = true;
      }
      if ($scope.run_details.contract_status === '5') {
        $scope.sell_button = true;
        $scope.diyan_button = true;
      }
      if ($scope.run_details.contract_status === '4' || $scope.run_details.contract_status === '5' || $scope.run_details.contract_status === '6') {
        $scope.tjd_button = true;
      }

    };

    $scope.defer = function() { //申请延期
      tradeApi
        .query({
          funcId: '300027',
          data: {
            contract_id: $scope.run_details.serial_no, // 协议编号
          }
        })
        .then(function(res) {
          alertTip.open({
            state: 1,
            msg: '延期申请提交成功'
          });
          $timeout(function() {
            $window.history.back();
          }, 1000);
        });
    };

    $scope.cover = function() { //申请市价平仓
      tradeApi
        .query({
          funcId: '300029',
          data: {
            contract_id: $scope.run_details.serial_no,
            entrust_price: $scope.run_details.last_price
          }
        })
        .then(function(res) {
          alertTip.open({
            state: 1,
            msg: '成功发出卖出指令'
          });
          $timeout(function() {
            $window.history.back();
          }, 1000);
        });
    };
    $scope.go_condition = function() {
      $state.go('condition', {
        type: '2',
        data: {
          source_contract_no: $scope.run_details.serial_no,
          borrow_balance: $scope.run_details.strategy_name,
          deposit_balance: $scope.run_details.deposit_balance,
          fare: $scope.run_details.synthesize_fare,
          prodcut_id: $scope.run_details.product_id,
          product_code: $scope.run_details.product_code,
          product_name: $scope.run_details.product_name,
          market: $scope.run_details.market,
        }
      })
    }


  });
})();